//File  :user.go

package apis

import (
	. "CatHomestay/database"
	"CatHomestay/models"
	"CatHomestay/tools"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

//用户注册
func UserRegister(c *gin.Context) {
	username := c.PostForm("username")
	password1 := c.PostForm("password1")
	password2 := c.PostForm("password2")

	if username == "" || password1 == "" || password2 == "" {
		c.JSON(http.StatusOK, gin.H{
			"msg": "字段不能为空",
		})
		return
	}

	if len(username) >= 10 && len(username) <= 3 {
		c.JSON(http.StatusOK, gin.H{
			"msg": "用户名要大于3个字符且小于10个字符",
		})
		return
	}

	if password1 != password2 {
		c.JSON(http.StatusOK, gin.H{
			"msg": "两次密码不一样，请重新输入",
		})
		return
	}
	var userinfo = models.UserInfo{Username: username,}

	var user = models.UserLogin{Username: username,
		Password: tools.MD5(password1),
		UserInfo: userinfo,
	}

	//判断用户是否存在
	res := Db.Where("username = ?", username).Take(&user)
	if res.RecordNotFound() {
		Db.Create(&user) //不存在,向数据表添加一条记录
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg": "用户名已存在",
		})
		return
	}
	c.JSON(http.StatusFound, gin.H{
		"msg": "注册成功",
	})
}

//用户登录
func UserLogin(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")

	pwd := tools.MD5(password)

	user := models.UserLogin{Username: username, Password: pwd}

	res := Db.Where("username = ?", username).Find(&user)
	res1 := Db.Where("username = ? and password = ?", username, pwd).Find(&user)
	if res.RecordNotFound() {
		c.JSON(http.StatusOK, gin.H{
			"msg": "用户不存在",
		})
		return
	}
	if res1.RecordNotFound() {
		c.JSON(http.StatusOK, gin.H{
			"msg": "密码错误",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"msg": "欢迎" + user.Username,
	})

}

//获取用户信息
func ShowInfo(c *gin.Context) {
	username := c.Param("username")

	userinfo := models.UserInfo{}

	res := Db.Where("username = ?", username).Take(&userinfo)

	if res.RecordNotFound() {
		c.JSON(http.StatusOK, gin.H{
			"msg": "用户" + username + "的信息未找到",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"Username": userinfo.Username,
			"Uid":      userinfo.UserUid,
			"Sex":      userinfo.Sex,
			"Address":  userinfo.Address,
			"Email":    userinfo.Email,
			"Mobile":   userinfo.Mobile,
		})
	}

}

//用户基础信息修改
func UserUpdate(c *gin.Context) {
	username := c.Param("username")

	sex := c.PostForm("sex")
	mobile := c.PostForm("mobile")
	email := c.PostForm("email")
	address := c.PostForm("address")

	userinfo := models.UserInfo{}
	res := Db.Where("username = ?", username).Take(&userinfo)

	if res.RecordNotFound() {
		c.JSON(http.StatusOK, gin.H{
			"msg": "用户不存在",
		})
		return
	} else {
		userinfo.Sex = sex
		userinfo.Address = address
		userinfo.Mobile = mobile
		userinfo.Email = email
		userinfo.UpdateAt = time.Now()

		err := Db.Save(&userinfo).Error
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"msg": "修改出错",
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"msg":      "用户信息修改成功",
			"username": userinfo.Username,
			"sex":      userinfo.Sex,
			"mobile":   userinfo.Mobile,
			"email":    userinfo.Email,
			"address":  userinfo.Address,
		})
	}

}

//用户密码修改
func PwdUpdate(c *gin.Context) {
	username := c.Param("username")

	password_old := c.PostForm("password_old")
	password_new := c.PostForm("password_new")

	userlogin := models.UserLogin{}

	res := Db.Where("username = ?", username).Take(&userlogin)
	if res.RecordNotFound() {
		c.JSON(http.StatusOK, gin.H{
			"msg": "用户名错误",
		})
		return
	}
	res1 := Db.Where("password = ?", tools.MD5(password_old)).Take(&userlogin)
	if res1.RecordNotFound() {
		c.JSON(http.StatusOK, gin.H{
			"msg": "原密码错误，请输入正确密码",
		})
		return
	}

	userlogin.Password = tools.MD5(password_new)
	err := Db.Save(&userlogin).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg": "修改密码失败，请重试",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"msg": "修改密码成功，请牢记您的新密码",
	})

}

//用户永久注销（删除）
func UserDelete(c *gin.Context) {
	username := c.Param("username")
	password := c.PostForm("password")

	userlogin := models.UserLogin{}
	userinfo := models.UserInfo{}

	res := Db.Where("username = ?", username).Take(&userlogin)
	if res.RecordNotFound() {
		c.JSON(http.StatusOK, gin.H{
			"msg": "用户名错误",
		})
		return
	}
	res1 := Db.Where("password = ?", tools.MD5(password)).Take(&userlogin)
	if res1.RecordNotFound() {
		c.JSON(http.StatusOK, gin.H{
			"msg": "密码错误，不能注销",
		})
		return
	}
	Db.Where("username = ?", username).Delete(&userlogin)
	Db.Where("username = ?", username).Delete(&userinfo)

	c.JSON(http.StatusOK, gin.H{
		"msg": "用户" + username + "注销成功",
	})
}
